package com.zone.vehicle.superApps.view.common.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.zone.vehicle.superApps.view.common.BackHandlerHelper;
import com.zone.vehicle.superApps.view.common.HandledFragment;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public abstract class BaseFragment<T extends HandledFragment> extends HandledFragment{

    protected static final String TAG = BaseFragment.class.getSimpleName();

    private Map<String, T> pages = new LinkedHashMap<>();
    private ViewPager viewPager;
//    private String defaultFragmentName;

    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initFragmentPages();
    }

    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
//        initDefaultFragment();
    }

//    public void setDefaultFragment(String defaultFragmentName){
//        this.defaultFragmentName = defaultFragmentName;
//    }
//    @Override
//    public void initDefaultFragment() {
//        if (StringUtils.isNotEmpty(defaultFragmentName)){
//            onPageChanged(defaultFragmentName);
//        }
//    }

    @Override
    public boolean onBackClicked(Fragment fragment) {
        return BackHandlerHelper.handleBackPress(fragment);
    }

//    @Override
//    public void onPageChanged(String pageName) {
//        T fragment = pages.get(pageName);
//        if (fragment != null && fragment.getClass().getSimpleName().equals(pageName)){
//            requireActivity().getSupportFragmentManager().beginTransaction()
//                    .replace(R.id.container, fragment)
//                    .addToBackStack(null)
//                    .commitNow();
//        }
//    }

    public Map<String, T> getPages() {
        return pages;
    }


    public T getPage(int position){
        List<T> list = new ArrayList<>(pages.values());
        return list.get(position);
    }

    public T getPage(String pageName){
        return pages.get(pageName);
    }

    public int getPagePosition(String pageName){
        int position = 0;
        for(String key: pages.keySet()){
            if (key.equals(pageName)){
                return position;
            }
            position ++;
        }
        return position;
    }

    public void addPage(T fragment) {
        pages.put(fragment.getClass().getSimpleName(), fragment);
    }


}
